package com.xw.llm;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.modelcontextprotocol.server.McpServer;
import io.modelcontextprotocol.server.McpServerFeatures;
import io.modelcontextprotocol.server.McpSyncServer;
import io.modelcontextprotocol.server.McpSyncServerExchange;
import io.modelcontextprotocol.server.transport.WebFluxSseServerTransportProvider;
import io.modelcontextprotocol.spec.McpSchema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;

import java.util.Map;
import java.util.function.BiFunction;

@Configuration
class McpServerConfig {
    private static final Logger log = LoggerFactory.getLogger(McpServerConfig.class);

    @Bean
    WebFluxSseServerTransportProvider webFluxSseServerTransport(ObjectMapper mapper) {
        return new WebFluxSseServerTransportProvider(mapper, "/mcp/message");
    }

    @Bean
    RouterFunction<?> mcpRouterFunction(WebFluxSseServerTransportProvider transport) {
        return transport.getRouterFunction();
    }


    @Bean(destroyMethod = "close")
    public McpSyncServer mcpSyncServer(WebFluxSseServerTransportProvider transportProvider) {
        McpSyncServer mcpSyncServer = McpServer.sync(transportProvider)
                .serverInfo("my-server", "1.0.0")
                .capabilities(McpSchema.ServerCapabilities.builder()
                        .tools(true)
                        .build())
                .build();

        String jsonSchema = """  
                {  
                	"type":"object",  
                	"properties":{  
                		"orderId" : {  
                			"type": "string",  
                			"description": "订单号"  
                		}  
                	},  
                	"required":[  
                		"orderId"  
                	],  
                	"additionalProperties":false  
                }  
                """;
        // 方法的描述
        McpSchema.Tool tool = new McpSchema.Tool("getOrderDetail", "通过订单号获取订单详情信息", jsonSchema);
        // 方法描述对应的执行逻辑
        BiFunction<McpSyncServerExchange, Map<String, Object>, McpSchema.CallToolResult> call = (exchange, arguments) -> {
//            OrderService.OrderDetail result = orderService.getOrderDetail(String.valueOf(arguments.get("orderId")));
            log.info("查询到订单信息");
            return new McpSchema.CallToolResult("测试订单基本信息\n" +
                    "订单编号：123456789\n" +
                    "下单时间：2025 年 06 月 16 日 14:30:22\n" +
                    "订单状态：已付款，待发货\n" +
                    "支付方式：微信支付\n" +
                    "买家信息\n" +
                    "姓名：张三\n" +
                    "联系电话：138****5678\n" +
                    "收货地址：XX 省 XX 市 XX 区 XX 街道 123 号 XX 小区 5 栋 2 单元 1001 室", false);
        };
        // 绑定描述和执行逻辑
        McpServerFeatures.SyncToolSpecification toolHandler = new McpServerFeatures.SyncToolSpecification(tool, call);

        // 添加到工具集中
        mcpSyncServer.addTool(toolHandler);

        return mcpSyncServer;
    }


}